﻿using Avalonia.Threading;
using FantomNext.Shared;
using FantomNext.Views;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;

namespace FantomNext.Presenters
{
    // 主要场景。
    public partial class ScenePresenter : IPresenter
    {
        #region props

        public Scene Model => _model;
        public SceneEditor View => _view;

        #endregion

        #region fields

        private Scene _model;
        private SceneEditor _view;

        #endregion

        #region props

        public IList<ShapePresenter> Shapes => _shapes;

        #endregion

        #region fields2

        private List<ShapePresenter> _shapes = new List<ShapePresenter>();

        #endregion

        #region ctors
        public ScenePresenter(SceneEditor sceneEditor, Scene model)
        {
            _view = sceneEditor;
            _model = model;

        }

        #endregion

        #region methods

        public void Load()
        {
            LoadView();
        }
        public async Task LoadAsync()
        {
            await LoadViewAsync();
        }

        /// <summary>
        /// 进行事件界面的绑定，开始进行事件的监听，尺寸的监听变化控制。
        /// </summary>
        private void LoadView()
        {
            var view = _view;
            var model = _model;

            var canvas = view.PART_FantomCanvas;
            var rect = view.PART_RectScene;

            UpdateScene(view, model, rect);

            UpdateCanvasTransform();

            view.SizeChanged += (s, e) =>
            {
                var view = _view;
                var model = _model;
                var rect = view.PART_RectScene;
                UpdateScene(view, model, rect);
            };

        }

      
        private async Task LoadViewAsync()
        {
            await Dispatcher.UIThread.InvokeAsync(() =>
            {
                LoadView();
            });
        }

        #endregion


    }
}
